package com.example.wzviewer.widget;

import android.content.Context;
import android.graphics.Color;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.IdRes;
import androidx.annotation.Nullable;
import androidx.media3.common.MediaItem;
import androidx.media3.common.Player;
import androidx.media3.exoplayer.ExoPlayer;
import androidx.media3.ui.PlayerView;
import com.example.wzviewer.R;


public class Media3VideoView extends FrameLayout{
    private PlayerView playerView;
    private ExoPlayer player;

    @Nullable
    private Uri currentUri; // 新增：记录正在播放的 Uri

    public Media3VideoView(Context context) {
        this(context,null);
    }

    //构造函数 初始化视图 并添加自定义控制条
    public Media3VideoView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setBackgroundColor(Color.BLACK); // 全屏黑色
        LayoutInflater.from(context).inflate(R.layout.widget_media3_video_view_inner, this, true);
        playerView = findViewById(R.id.player_view_inner);
        // 可选：控制条自动隐藏时长（若需要）
        // playerView.setControllerShowTimeoutMs(3000); // 需要 @OptIn(UnstableApi.class)

/*
        setLayoutParams(new LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        //初始化PlayerView
        playerView = new PlayerView(context);
        //设置PlayerView的布局参数
        playerView.setLayoutParams(new LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        playerView.setUseController(true); // 显示控制条（可关）

        addView(playerView);
*/

    }


    //用于播放mp4/mov/ts/hls...等等。可以本地或者网络
    public void play(Uri uri){
        ensurePlayer();
        //关闭自动播放并暂停
        player.setPlayWhenReady(false);
        player.pause();
        this.currentUri = uri; //记住当前的uri
        player.setMediaItem(MediaItem.fromUri(uri));
        player.prepare();
        //player.play(); //只准备，不播放
    }

    //判断是否正在播放
    public boolean isPlaying(){
        if (player!=null){
            return player.isPlaying();  //直接调用ExoPlayer的方法
        }else{
            return false;
        }
    }

    //播放 不带参数
    public void play(){
        if(player!=null){
            player.play();
        }
    }

    //暂停
    public void pause(){
        if(player!=null){
            player.pause();
        }
    }

    //恢复播放
    public void resumePlay(){
        if(player!=null){
            player.play();
        }
    }

    //释放播放器
    public void release(){
        if(player!=null){
            player.release();
            player = null;
        }
    }



    private void ensurePlayer() {
        if(player==null){
            System.out.println("无播放对象");

            //如果没有播放器，就创建一个新的ExoPlayer并绑定到PlayerView上
            player = new ExoPlayer.Builder(getContext()).build();
            playerView.setPlayer(player);
        }
    }

    //获得视频总时长
    public long getDuration() {
        return player != null ? player.getDuration() : 0;
    }


    public View findInController(@IdRes int id) {
        return playerView.findViewById(id);
    }

    //允许外部注册监听
    public void addPlayerListener(Player.Listener listener) {
        ensurePlayer();
        player.addListener(listener);   //将实现类对象添加到ExoPlayer的监听列表中，当ExoPlayer的状态发生改变时，会调用实现类的方法
    }

    // 暴露播放状态，给PlayerActivity使用
    public int getPlaybackState() {
        return player != null ? player.getPlaybackState() : Player.STATE_IDLE;
    }

    public void seekTo(long positionMs) {
        if (player != null) player.seekTo(positionMs);
    }

    // 新增：对外暴露当前 Uri
    @Nullable
    public Uri getCurrentUri() {
        return currentUri;
    }

    // 新增：对外暴露当前播放位置（ms）
    public long getCurrentPosition() {
        return player != null ? player.getCurrentPosition() : 0;
    }


}
